#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<string.h>
//#dedine NUM 128
int main()
{
  char command[128]={};
  while(1)
  {
    char *argv[64]={NULL};
    command[0]=0;
    printf("[who@myhostname mydir]# ");
    fgets(command,128,stdin);
    command[strlen(command)-1]='\0';
    printf("echo: %s\n",command);
    fflush(stdout);



    const char *sep=" ";
    int i=1;
    argv[0]=strtok(command,sep);
    while(argv[i]=strtok(NULL,sep))
    {
      i++;
    }



    if(strcmp(argv[0],"cd")==0)
    {
      if(argv[1]!=NULL)
        chdir(argv[1]);
      continue;
    }

    //执行第三方命令  
    if(fork()==0)
    {
      execvp(argv[0],argv);
      exit(1);
    }
    int status=0;
    waitpid(-1,&status,0);
    printf("exit code: %d\n",(status>>8)&0xff);
  }
}
